//
//  YyxSegementController.m
//  didacar
//
//  Created by 度周末网络-王腾 on 15/12/3.
//  Copyright © 2015年 dzmmac. All rights reserved.
//

#import "YyxSegementController.h"

@interface YyxSegementController () <UIPageViewControllerDataSource,UIPageViewControllerDelegate>

@property (strong, nonatomic) UIPageViewController * pageController;

@end

@implementation YyxSegementController

-(void)initPageController{
    NSDictionary * options = [NSDictionary dictionaryWithObject:[NSNumber numberWithInteger:UIPageViewControllerSpineLocationMax] forKey:UIPageViewControllerOptionInterPageSpacingKey];
    
    self.pageController = [[UIPageViewController alloc]initWithTransitionStyle:(UIPageViewControllerTransitionStyleScroll) navigationOrientation:(UIPageViewControllerNavigationOrientationHorizontal) options:options];
    self.pageController.dataSource = self;
    self.pageController.delegate   = self;
    self.pageController.view.backgroundColor = [UIColor clearColor];
    self.pageController.view.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
 
    [self addSubview:self.pageController.view];
    
    NSArray *pviewControllers = [NSArray arrayWithObjects:[self.viewControllers firstObject],nil];
    [self.pageController setViewControllers:pviewControllers direction:(UIPageViewControllerNavigationDirectionReverse) animated:NO completion:nil];
    
    for (UIView *v in self.pageController.view.subviews) {
        if ([v isKindOfClass:[UIScrollView class]]) {
            ((UIScrollView *)v).bounces = NO;
        }
    }
}

-(void)selectIndex:(NSInteger)index{
    
    UIPageViewControllerNavigationDirection direction = UIPageViewControllerNavigationDirectionForward;
    
    if (self.currentIndex > index) {
        direction = UIPageViewControllerNavigationDirectionReverse;
    }
    self.currentIndex = index;
    NSArray *pviewControllers = [NSArray arrayWithObjects:[self.viewControllers objectAtIndex:index],nil];
    [self.pageController setViewControllers:pviewControllers direction:direction animated:YES completion:nil];
}

-(instancetype)initWithFrame:(CGRect)frame viewControllers:(NSArray *)viewControllers changeBlock:(SegementControllerChangeBlock)changeBlock{
    self = [super init];
    if (self){
        self.frame = frame;
        self.segementControllerChangeBlock = changeBlock;
        self.viewControllers = viewControllers;
        [self initPageController];
    }
    return self;
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController{
    
    NSUInteger index = [self.viewControllers indexOfObject:viewController];
    
    if (index == NSNotFound) {
        return nil;
    }
    self.currentIndex = index;
    if (self.segementControllerChangeBlock) {
        self.segementControllerChangeBlock(index);
    }
  
    index++;
    if (index == [self.viewControllers count]) {
        return nil;
    }
    
    
    return [self.viewControllers objectAtIndex:index];
}

- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController{
    
    NSUInteger index = [self.viewControllers indexOfObject:viewController];
    self.currentIndex = index;
    if (self.segementControllerChangeBlock) {
        self.segementControllerChangeBlock(index);
    }
    
    if (index == 0 || (index == NSNotFound)) {
        return nil;
    }
    index --;
    
    return [self.viewControllers objectAtIndex:index];
}

@end
